﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


abstract class doTop
{
    virtual public void Do(bugData bd)
    {
    }

    public string doName;

}

class wait : doTop
{
    public raimuCore Owner = null;

    public wait()
    {
        this.doName = "WAIT";
    }
    public override void Do(bugData me)
    {
        me.oldHp = me.hp;
        if (me.goORstop())
        {

            me.hp = 100;
            Random rnd = new Random();
            if (rnd.Next() % 500 == 0)
            {
                me.group = rnd.Next(1, 3);
                me.changed++;

            }
            me.AddNeuronSuccess();
        }
        else
        {
            Debug.Message("NONE:WAIT");
        }
    }
}

class kill : doTop
{
    public raimuCore Owner = null;
    public kill()
    {
        this.doName = "KILL";
    }
    public override void Do(bugData me)
    {
        Random rnd = new Random();

        List<int> tmp2 = new List<int>();
        me.oldHp = me.hp;

        int iss = Owner.getListCount() - Owner.getListCount() / 2;
        if (me.goORstop())
        {
            for (; iss > 0; iss--)
            {

                int index = rnd.Next(Owner.getListCount());
                if (tmp2.IndexOf(index) == -1)
                {
                    List<bugData> list = Owner.getList();

                    if (me.group != list[index].group)
                    {
                        if (list[index].id.Length < me.id.Length)
                        {

                            Owner.DeleteAt(index);
                            me.hp -= 15;

                            if (me.hp <= 0)
                            {
                                break;
                            }
                        }
                        else if (list[index].id.Length == me.id.Length)
                        {
                            if (rnd.Next() % 2 == 0)
                            {
                                me.id += "P";
                            }
                            else
                            {
                                list[index].id += "O";
                            }
                            iss++;
                        }
                        else
                        {
                            me.hp -= 25;
                        }
                        tmp2.Add(index);
                    }
                    else
                    {
                        iss++;
                    }
                }

                if (rnd.Next() % 500 == 0)
                {
                    me.group = rnd.Next(1, 3);
                    me.changed++;

                }
            }
            me.AddNeuronSuccess();
        }

        else
        {
            Debug.Message("NONE:KILL");
        }
    }
}

class eat : doTop
{
    public raimuCore Owner = null;
    public eat()
    {
        this.doName = "EAT";
    }

    public override void Do(bugData me)
    {
        if (me.goORstop())
        {
            Random rnd = new Random();

            int index = rnd.Next(Owner.getListCount());
            List<bugData> list = new List<bugData>();

            if (list[index].id.Length < me.id.Length)
            {
                Owner.Delete(list[index]);
            }
            else
            {
                me.AddNeuron(me);
            }

            if (rnd.Next() % 500 == 0)
            {
                me.group = rnd.Next(1, 3);
                me.changed++;
            }
            me.AddNeuronSuccess();
        }
        else
        {
            Debug.Message("NONE:EAT");
        }
    }
}
class None : doTop
{
    public None()
    {
        this.doName = "CLONE";
    }
}

class add : doTop
{
    public raimuCore Owner = null;
    public add()
    {
        this.doName = "ADD";
    }
    public override void Do(bugData me)
    {

        Random rnd = new Random();
        me.oldHp = me.hp;
        if (me.goORstop())
        {

            if (rnd.Next() % 2 == 0)
            {
                bugData meClone = new bugData();
                meClone.hp = me.hp / 2;
                meClone.addList(new None());
                meClone.id = me.id.Replace(me.id[me.id.Length - 1], 'C');
                meClone.group = me.group;
                meClone.AddNeuron(me.getNeuron());
                Owner.addList(meClone);
                me.hp /= 2;


                if (rnd.Next() % 500 == 0)
                {
                    me.group = rnd.Next(1, 3);
                    me.changed++;
                }

            }
            me.AddNeuronSuccess();

        }
        else
        {
            Debug.Message("NONE:ADD");
        }
    }
}